//
//  AddFriendVerifyViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/26.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AddFriendVerifyViewController.h"

@interface AddFriendVerifyViewController ()
/**
 title
 */
@property (nonatomic , strong) UILabel *titleLab;
/**
 textFieldBgView
 */
@property (nonatomic , strong) UIView *textFieldBgView;
/**
 textField
 */
@property (nonatomic , strong) UITextField *textField;
/**
 确定
 */
@property (nonatomic , strong) UIButton *confirmBtn;
@end

@implementation AddFriendVerifyViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"好友验证";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick)];
}


- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.titleLab];
    [self.view addSubview:self.textFieldBgView];
    [self.textFieldBgView addSubview:self.textField];
    [self.view addSubview:self.confirmBtn];
}

- (void) initLayout {
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(18);
        make.right.mas_equalTo(-10);
    }];
    [self.textFieldBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(5);
        make.height.mas_equalTo(56);
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(48 * [JTool getScreenWidthScale]);
        make.top.mas_equalTo(self.textFieldBgView.mas_bottom).offset(90 * [JTool getScreenWidthScale]);
    }];
}

- (void) initAction {
    [self.confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)initData {
    
}

#pragma mark - delegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.text.length > 15) {
        textField.text = [textField.text substringToIndex:15];
    }
}
#pragma mark - target action
//添加好友腾讯云请求
- (void)confirmBtnClick {
    NSString *addWording = self.textField.text;
    TIMFriendRequest *req = [[TIMFriendRequest alloc] init];
    req.identifier = self.identifier;
    req.addSource = @"AddSource_Type_iOS";
    req.addWording = addWording;
    @weakify(self);
    [[TIMFriendshipManager sharedInstance] addFriend:req succ:^(TIMFriendResult *result) {
        @strongify(self);
        [self toastWithContent:@"添加成功,等待对方验证"];
        [self.navigationController popViewControllerAnimated:YES];
    } fail:^(int code, NSString *msg) {
        @strongify(self);
        [self toastWithContent:msg];
    }];
}
- (void)leftButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - private method

#pragma mark - getter and setter
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor colorWithHexString:@"#B0B0B0"];
        _titleLab.font = [UIFont systemFontOfSize:13];
        _titleLab.text = @"发送验证申请，等待对方通过";
    }
    return _titleLab;
}
- (UIView *)textFieldBgView {
    if (!_textFieldBgView) {
        _textFieldBgView = [[UIView alloc] init];
        _textFieldBgView.backgroundColor = [UIColor whiteColor];
    }
    return _textFieldBgView;
}
- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        [_textField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        [_textField setValue:[UIColor colorWithHexString:@"#B0B0B0"] forKeyPath:@"_placeholderLabel.textColor"];
        _textField.placeholder = @"我是...";
        _textField.clearButtonMode = UITextFieldViewModeAlways;
    }
    return _textField;
}
- (UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"login_loginbtn_default"] forState:UIControlStateNormal];
    }
    return _confirmBtn;
}

@end
